package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn0000Bo;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn00120Bo;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class ChangHaiInputResultController extends TmsController {
	private Logger logger = Logger
			.getLogger(ChangHaiInputResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferRgInputViewHelper view = (TransferRgInputViewHelper)uc.getAttribute("changhaixian");
			TmsUserProfile profile = uc.getUserProfile();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			Car car = Car.getInstance();
			car = car.searchAllCarByLicensePlate(view.getLicensePlate());
			TransferProcessHistory his = new TransferProcessHistory();
			TransferPersonalCarInfo personal = new TransferPersonalCarInfo();
			
			if (car != null) {
				//modify by huangheli at 20150905 start
				//if (car.getRegisterStatus() == 1 || car.getRegisterStatus() == 2) {
				if (car.getRegisterStatus() != 3 && car.getRegisterStatus() != 4) {
				//modify by huangheli at 20150905 end
					//过户表
					Sequence sq = Sequence.getInstance();
					accept = accept.getAcceptInfoBySerialAndCarLicense("", view.getLicensePlate(), 0);
					String serial = accept.getSerial();
					accept.setMarketType(1);
					accept.setLicensePlate(view.getLicensePlate());
					accept.setStatus(3);
					accept.setLc_printNum(1);
					accept.setLc_printUserId(profile.getUniqueId());
					accept.setCarClass(2);
					accept.setSourceType(2);
					accept.setCy_cardType(view.getCardType());
					accept.setCarOwner(view.getCarOwner());
					accept.setOwnerType(Integer.parseInt(view.getOwnerType()));
					accept.setVin(car.getVin());
					accept.setBasicCarId(car.getSerial());
					accept.setInsertTime(DateUtil.getStringToTimeStamp1(DateUtil.getCurrentTime()));
					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						accept.setTransferPrice(Integer.parseInt(view.getTransferPrice()));
					}else{
						accept.setTransferPrice(0);
					}
					accept.updateAcceptInfoAtPad(accept);
					TransferAcceptedInfo accept1 = new TransferAcceptedInfo();
					accept1.setLc_printNum(1);
					accept1.setLc_printUserId(profile.getUniqueId());
					accept1.setUpdateUserId(profile.getUniqueId());
					accept1.setSerial(serial);
					accept1.updatePrintInfoBySerialLc(accept1);

					//历史表
					his.setSerial(serial);
					his.setBusinessType(2);
					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						his.setBusinessType(8);
					}

					his.setReason(view.getReason());
					his.setInsertUserId(profile.getUniqueId());
					his.insertHisInfoAtAssess(his);
					//库存表
					car.setSerial(car.getSerial());
					car.setStatus(3);
					car.setUpdateUserId(profile.getUniqueId());
					car.setCardType(Integer.parseInt(view.getCardType()));
					car.setCarOwner(view.getCarOwner());
					car.setOwnerType(Integer.parseInt(view.getOwnerType()));	
					car.setLicensePlate(view.getLicensePlate());
					car.updateMarketInfoByLicensePlate(car);
					
					view.setSerial(serial);
					
					TransferFinanceInfo upFinance = new TransferFinanceInfo();
					upFinance.setSerial(serial);
					
					//modify by huangwei
					if(!StringUtil.isNullOrBlank(view.getChargeType())){
						upFinance.setCheckPriceChargeType(Integer.parseInt(view.getChargeType()));
						upFinance.setTransferPriceChargeType(Integer.parseInt(view.getChargeType()));
					}
					upFinance.setCheckPrice(Integer.parseInt(view.getCheckPrice()));
					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						 upFinance.setTransferPrice(Integer.parseInt(view.getTransferPrice()));
					}else{
						 upFinance.setTransferPrice(0);
					}
					upFinance.setCheckPriceUserId(profile.getUniqueId());

					upFinance.setCheckPriceChargeType(1);
					upFinance.setTransferPriceChargeType(1);
					upFinance.updateFinanceInfoAtCheck(upFinance);

					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						TransferFinanceInfo transFinance = new TransferFinanceInfo();
						transFinance.setSerial(serial);
						transFinance.setTransferPriceUserId(profile.getUniqueId());
						transFinance.updateTransferTime(transFinance);
					}
					
//					workload:3.1.2 delete
//					// add by huangheli 2016/01/09 start
//					MobileMessage message = new MobileMessage();
//					message.putValue("Serial", accept.getSerial());
//					message.setMessage("过户同步[长海县车辆]接口,过户流水号为#Serial#过户信息不存在，无法同步TMS的长海县（更新）");
//					transferDataSyn00120Bo bo = new transferDataSyn00120Bo();
//					bo.setMobileMessage(message);
//					bo.setSerial(accept.getSerial());
//					bo.setFunc(12);
//					bo.setStatus(2);
//					bo.setDetailStatus(3);
//					PullingExecutor.getInstance().submit(bo);	
//					// add by huangheli 2016/01/09 end
				}else {
					//过户表
					Sequence sq = Sequence.getInstance();
					String maxSerial = sq.getSerial(6);	
					String personSerial = sq.getSerial(5);
					accept.setSerial(maxSerial);
					accept.setMarketType(2);
					accept.setLicensePlate(view.getLicensePlate());
					accept.setStatus(3);
					accept.setLc_printNum(1);
					accept.setLc_printUserId(profile.getUniqueId());
					accept.setCarClass(2);
					accept.setSourceType(2);
					accept.setCy_cardType(view.getCardType());
					accept.setCarOwner(view.getCarOwner());
					accept.setOwnerType(Integer.parseInt(view.getOwnerType()));
					accept.setVin(car.getVin());
					accept.setBasicCarId(personSerial);
					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						accept.setTransferPrice(Integer.parseInt(view.getTransferPrice()));
					}else{
						accept.setTransferPrice(0);
					}
					accept.insertCarInfoAtChx(accept);
					
					view.setSerial(maxSerial);

					//历史表
					his.setSerial(maxSerial);
					his.setBusinessType(2);

					his.setReason(view.getReason());
					his.setInsertUserId(profile.getUniqueId());
					his.insertHisInfoAtAssess(his);
					
					//非库存表
					personal.setSerial(personSerial);
					personal.setStatus(3);
					personal.setUpdateUserId(profile.getUniqueId());
					personal.setCardType(Integer.parseInt(view.getCardType()));
					personal.setCarOwner(view.getCarOwner());
					personal.setOwnerType(Integer.parseInt(view.getOwnerType()));	
					personal.setLicensePlate(view.getLicensePlate());
					personal.insertCarBaseInfoAtAssess(personal);
					
					//tms_transfer_finance_info
					TransferFinanceInfo finance = new TransferFinanceInfo();
					finance.insertFinanceByInner(maxSerial);
					
					TransferFinanceInfo upFinance = new TransferFinanceInfo();
					upFinance.setSerial(maxSerial);
					
					//modify by huangwei
					if(!StringUtil.isNullOrBlank(view.getChargeType())){
						upFinance.setCheckPriceChargeType(Integer.parseInt(view.getChargeType()));
						upFinance.setTransferPriceChargeType(Integer.parseInt(view.getChargeType()));
					}
					upFinance.setCheckPrice(Integer.parseInt(view.getCheckPrice()));
					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						 upFinance.setTransferPrice(Integer.parseInt(view.getTransferPrice()));
					}else{
						 upFinance.setTransferPrice(0);
					}
					upFinance.setCheckPriceUserId(profile.getUniqueId());
					upFinance.setCheckPriceChargeType(1);
					upFinance.setTransferPriceChargeType(1);
					upFinance.updateFinanceInfoAtCheck(upFinance);

					if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
					{
						TransferFinanceInfo transFinance = new TransferFinanceInfo();
						transFinance.setSerial(maxSerial);
						transFinance.setTransferPriceUserId(profile.getUniqueId());
						transFinance.updateTransferTime(transFinance);
					}
				}
			}else {
				//过户表
				Sequence sq = Sequence.getInstance();
				String maxSerial = sq.getSerial(6);
				String personSerial = sq.getSerial(5);
				accept.setSerial(maxSerial);
				accept.setMarketType(2);
				accept.setLicensePlate(view.getLicensePlate());
				accept.setStatus(3);
				accept.setLc_printNum(1);
				accept.setLc_printUserId(profile.getUniqueId());
				accept.setCarClass(2);
				accept.setSourceType(2);
				accept.setCy_cardType(view.getCardType());
				accept.setCarOwner(view.getCarOwner());
				accept.setOwnerType(Integer.parseInt(view.getOwnerType()));
				accept.setBasicCarId(personSerial);
				if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
				{
					accept.setTransferPrice(Integer.parseInt(view.getTransferPrice()));
				}else{
					accept.setTransferPrice(0);
				}
				accept.insertCarInfoAtChx(accept); 
				
				view.setSerial(maxSerial);

				// 历史记录
				his.setSerial(maxSerial);
				his.setBusinessType(2);
				his.setReason(view.getReason());
				his.setInsertUserId(profile.getUniqueId());
				his.insertHisInfoAtAssess(his);
				
				//非库存表
				personal.setSerial(personSerial);
				personal.setStatus(3);
				personal.setUpdateUserId(profile.getUniqueId());
				personal.setCardType(Integer.parseInt(view.getCardType()));
				personal.setCarOwner(view.getCarOwner());
				personal.setOwnerType(Integer.parseInt(view.getOwnerType()));	
				personal.setLicensePlate(view.getLicensePlate());
				personal.insertCarBaseInfoAtAssess(personal);
				
				//tms_transfer_finance_info
				TransferFinanceInfo finance = new TransferFinanceInfo();
				finance.insertFinanceByInner(maxSerial);
				
				TransferFinanceInfo upFinance = new TransferFinanceInfo();
				upFinance.setSerial(maxSerial);
				
				//modify by huangwei
				if(!StringUtil.isNullOrBlank(view.getChargeType())){
					upFinance.setCheckPriceChargeType(Integer.parseInt(view.getChargeType()));
					upFinance.setTransferPriceChargeType(Integer.parseInt(view.getChargeType()));
				}
				
				upFinance.setCheckPrice(Integer.parseInt(view.getCheckPrice()));
				if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
				{
					 upFinance.setTransferPrice(Integer.parseInt(view.getTransferPrice()));
				}else{
					 upFinance.setTransferPrice(0);
				}
				upFinance.setCheckPriceUserId(profile.getUniqueId());

				upFinance.setCheckPriceChargeType(1);
				upFinance.setTransferPriceChargeType(1);
				upFinance.updateFinanceInfoAtCheck(upFinance);

				if(view.getTransferPrice()!=null && !"".equals(view.getTransferPrice()))
				{
					TransferFinanceInfo transFinance = new TransferFinanceInfo();
					transFinance.setSerial(maxSerial);
					transFinance.setTransferPriceUserId(profile.getUniqueId());
					transFinance.updateTransferTime(transFinance);
				}
			}

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
